/*
 *
 * Copyright 2017-2018 549477611@qq.com(xiaoyu)
 *
 * This copyrighted material is made available to anyone wishing to use, modify,
 * copy, or redistribute it subject to the terms and conditions of the GNU
 * Lesser General Public License, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
 * for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this distribution; if not, see <http://www.gnu.org/licenses/>.
 *
 */

package com.github.myth.common.utils;

import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * <p>Description: .</p>
 *
 * @author xiaoyu(Myth)
 * @version 1.0
 * @date 2017/11/8 14:25
 * @since JDK 1.8
 */
public class FileUtils {

    public static void writeFile(String fullFileName, byte[] contents) throws Exception {
        RandomAccessFile raf = new RandomAccessFile(fullFileName, "rw");
        FileChannel channel = raf.getChannel();
        try {
            ByteBuffer buffer = ByteBuffer.allocate(contents.length);
            buffer.clear();
            buffer.put(contents);
            buffer.flip();
            while (buffer.hasRemaining()) {
                channel.write(buffer);
            }
            channel.force(true);
        } catch (Exception e) {
            throw e;
        } finally {
            if (channel != null) {
                channel.close();
            }
        }
    }
}
